package com.example.compete.service.impl;

import com.example.compete.DTO.EducationTypeDTO;
import com.example.compete.DTO.ExecutiveDTO;
import com.example.compete.DTO.PostTypeDTO;
import com.example.compete.DTO.SexTypeDTO;
import com.example.compete.mapper.ExecutiveMapper;
import com.example.compete.service.ExecutiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ExecutiveServiceImpl implements ExecutiveService {

    @Autowired
    private ExecutiveMapper executiveMapper;

    private ExecutiveDTO executiveDTO;


  //查询高管所有信息
    @Override
    public List<ExecutiveDTO> findAll() {
        List<ExecutiveDTO> executiveDTOList = executiveMapper.findAll();
        for (ExecutiveDTO executiveDTO:executiveDTOList) {
            String postName = executiveMapper.findPostName(executiveDTO.getPost());
            String sexName = executiveMapper.findSexName(executiveDTO.getSex());
            String educationName = executiveMapper.findEducationName(executiveDTO.getEducation());
            executiveDTO.setPostName(postName);
            executiveDTO.setSexName(sexName);
            executiveDTO.setEducationName(educationName);
        }
        return executiveDTOList;
    }

    //根据高管编号查信息
    @Override
    public ExecutiveDTO findByExecutiveId(String executiveId) {
    	 ExecutiveDTO executiveDTO=executiveMapper.findByExecutiveId(executiveId);
    	 String postName = executiveMapper.findPostName(executiveDTO.getPost());
         String sexName = executiveMapper.findSexName(executiveDTO.getSex());
         String educationName = executiveMapper.findEducationName(executiveDTO.getEducation());
         executiveDTO.setPostName(postName);
         executiveDTO.setSexName(sexName);
         executiveDTO.setEducationName(educationName);
        return executiveDTO;
    }

    
    //添加高管信息
    @Override
    public void add(ExecutiveDTO executiveDTO) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<ExecutiveDTO> executiveDTOS = executiveMapper.findAll();
        String lastExecutiveId=null;
        String count=null;
        if(executiveDTOS.size()==0){
            lastExecutiveId="M0000000000";
        }else{
            lastExecutiveId=executiveDTOS.get(executiveDTOS.size()-1).getExecutiveId();
        }
        char[] executiveIdChar=lastExecutiveId.toCharArray();
        executiveIdChar[0]='0';
        count=String.valueOf(executiveIdChar);
        int countInt=Integer.valueOf(count);
        StringBuffer countStringBuffer=new StringBuffer(String.valueOf(countInt+1));
        while(true){
            if(countStringBuffer.length()==10){
                break;
            }else{
                countStringBuffer.insert(0, '0');
            }
        }
        countStringBuffer.insert(0, 'M');
        executiveDTO.setExecutiveId(countStringBuffer.toString());
        executiveDTO.setRegisteredTime(simpleDateFormat.format(new Date()));
        executiveDTO.setUpdateTime(simpleDateFormat.format(new Date()));
        executiveMapper.add(executiveDTO);
    }

    //修改高管信息
    @Override
    public void updateExecutive(ExecutiveDTO executiveDTO) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        executiveDTO.setUpdateTime(simpleDateFormat.format(new Date()));
        executiveMapper.updateExecutive(executiveDTO);
    }

    //根据高管编号删除高管信息
    @Override
    public int deleteByExecutiveId(String executiveId) {
        return executiveMapper.deleteByExecutiveId(executiveId);
    }

    //查找所有学历信息
	@Override
	public List<EducationTypeDTO> findAllEducationType() {
		// TODO Auto-generated method stub
		return executiveMapper.findAllEducationType();
	}

	//查找所有职务信息
	@Override
	public List<PostTypeDTO> findAllPostType() {
		// TODO Auto-generated method stub
		return executiveMapper.findAllPostType();
	}

	//查找所有性别信息
	@Override
	public List<SexTypeDTO> findAllSexType() {
		// TODO Auto-generated method stub
		return executiveMapper.findAllSexType();
	}

	//根据公司编号查找所有高管信息
	@Override
	public List<ExecutiveDTO> selectAllByCompanyId(String companyId) {
		// TODO Auto-generated method stub
		List<ExecutiveDTO> executiveDTOList = executiveMapper.selectAllByCompanyId(companyId);
        for (ExecutiveDTO executiveDTO:executiveDTOList) {
            String postName = executiveMapper.findPostName(executiveDTO.getPost());
            String sexName = executiveMapper.findSexName(executiveDTO.getSex());
            String educationName = executiveMapper.findEducationName(executiveDTO.getEducation());
            executiveDTO.setPostName(postName);
            executiveDTO.setSexName(sexName);
            executiveDTO.setEducationName(educationName);
        }
        return executiveDTOList;
	}
}
